import AntModal from '@/components/pt/dialog/AntModal'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import { addBizPayable, updateBizPayable } from '@/api/biz/bizPayable'
import { getBizSupplierPayable } from '@/api/biz/bizSupplierPayable'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'CreateForm',
  props: {
    supplierOptions: {
      type: Array,
      required: true
    },
    payOptions: {
      type: Array,
      required: true
    },
  },
  components: {
    AntModal,
    AdvanceTable
  },
  data () {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '新增付款单',
      // 表单参数
      form: {},
      rules: {
        customerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],

        orderType: [{ required: true, message: '订单类型不能为空', trigger: 'blur' }]
      }
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {
    "form.supplierCode": {
      handler(val) {
        if (val) {
          this.$nextTick(() => (
            getBizSupplierPayable(val).then(response => {

              this.form.hasAmount = response.data &&  response.data.hasAmount || 0
              this.$forceUpdate();
            })
          ))
        }
      },
      immediate: true,
    },
  },
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,

        payCode: undefined,

        supplierCode: undefined,

        supplierName: undefined,

        payAmount: undefined,

        purchaseDate: undefined,

        hasAmount: undefined,

        remark: undefined,

        payDate: undefined,

        payAccount: undefined

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.open = true
      this.formTitle = row.id ? '修改付款单' : '新增付款单';
      this.form = row
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          this.form.payType = 2
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            updateBizPayable(saveForm).then(response => {
              this.$message.success('更新成功', 3)
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
            })
          } else {
            addBizPayable(saveForm).then(response => {
              this.$message.success('新增成功', 3)
                this.$emit('ok')
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/biz/bizpayable/index'
      this.$router.push(index)
    },

  }
}
